package com.xindaoapp.happypet.adapter;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.bean.Founds.Found;
import com.xindaoapp.happypet.utils.CommonParameter;

public class FoundsAdapter extends BaseAdapter {
    private final List<Found> arrays = new ArrayList<Found>();
    private final Context mContext;

    public FoundsAdapter(Context mContext) {
        super();
        this.mContext = mContext;
    }

    public void addItems(List<Found> items) {
        arrays.addAll(items);
    }

    public void clearItems() {
        arrays.clear();
    }

    public List<Found> getAll() {
        return arrays;
    }

    @Override
    public int getCount() {
        return arrays.size();
    }

    @Override
    public Object getItem(int position) {
        return arrays.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(mContext, R.layout.active_list_item, null);
            holder.coverpath = (ImageView) convertView.findViewById(R.id.coverpath);
            holder.subject = (TextView) convertView.findViewById(R.id.subject);
            holder.starttimefrom = (TextView) convertView.findViewById(R.id.starttimefrom);
            holder.place = (TextView) convertView.findViewById(R.id.place);
            holder.view = (TextView) convertView.findViewById(R.id.view);
            holder.applynumber = (TextView) convertView.findViewById(R.id.applynumber);
            holder.expiration = (TextView) convertView.findViewById(R.id.expiration);
            holder.isbegin = (TextView) convertView.findViewById(R.id.isbegin);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (!TextUtils.isEmpty(arrays.get(position).coverpath)) {
            if (CommonParameter.isLoadImg) {
                ImageLoader.getInstance().displayImage(arrays.get(position).coverpath,
                        holder.coverpath);
            } else {
                holder.coverpath.setImageResource(R.drawable.picture_loading);
            }
        }
        holder.subject.setText(arrays.get(position).subject);
        holder.starttimefrom.setText("时间：" + arrays.get(position).starttimefrom);
        holder.place.setText("地点：" + arrays.get(position).place);
        holder.view.setText(arrays.get(position).views);
        holder.applynumber.setText(arrays.get(position).applynumber);
        if (arrays.get(position).expiration.equals("0")) {
            holder.expiration.setText(arrays.get(position).expiration);
            holder.expiration.setVisibility(View.GONE);
            holder.isbegin.setText("进行中");
        } else {
            holder.expiration.setVisibility(View.VISIBLE);
            holder.expiration.setText("截止时间：" + arrays.get(position).expiration);

            if (formatTime(arrays.get(position).expiration) > System.currentTimeMillis()) {
                holder.isbegin.setText("已结束");
            } else if (formatTime(arrays.get(position).starttimefrom) > System.currentTimeMillis()) {
                holder.isbegin.setText("未开始");
            } else {
                holder.isbegin.setText("进行中");
            }
        }
        return convertView;
    }

    public long formatTime(String s) {
        try {
            DateFormat format = new SimpleDateFormat("yyyy-MM-DD hh:mm:ss");
            return format.parse(s).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    class ViewHolder {
        ImageView coverpath;
        TextView subject;
        TextView starttimefrom;
        TextView place;
        TextView view;
        TextView applynumber;
        TextView expiration;
        TextView isbegin;
    }
}
